﻿/***********************************************************************
 * <copyright file="AdminBaseController.cs" company="B/S JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   Hoang Bich Son 
 * Email:    sonhb@newman.vn
 * Website:
 * Create Date: Monday, March 10, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System.Web.Mvc;
using DxMvcApplication.Areas.Administrator.Models;
using DxMvcApplication.Code;
using DxMvcApplication.Repositories.Core.Security;
using DxMvcApplication.Repositories.Implementations.Security;
using Newtonsoft.Json;

namespace DxMvcApplication.Areas.Administrator.Controllers
{
    /// <summary>
    /// AdminBaseController class
    /// </summary>
    public class AdminBaseController : Controller
    {
        /// <summary>
        /// The _site repository
        /// </summary>
        private readonly ISiteRepository _siteRepository;

        /// <summary>
        /// The _permission repository
        /// </summary>
        private readonly IPermissionRepository _permissionRepository;

        /// <summary>
        /// The unit identifier
        /// </summary>
        protected long UnitId;

        /// <summary>
        /// The user identifier
        /// </summary>
        protected int UserId;

        /// <summary>
        /// The site identifier
        /// </summary>
        protected int SiteId = 0;

        /// <summary>
        /// The per identifier
        /// </summary>
        protected int PerId = 0;

        /// <summary>
        /// Called before the action method is invoked.
        /// </summary>
        /// <param name="filterContext">Information about the current request and action.</param>
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            
            var o = Session["UserId"];
            var unitId = Session["UnitId"];
            if(unitId!=null)
            {                
                UnitId = long.Parse(unitId.ToString());
            }
            if (o == null)
            {
                filterContext.Result = Redirect(UrlMaker.ToLogin());
            }
            else
            {
                int.TryParse(o.ToString(), out UserId);
                if (UserId > 0)
                {
                    var siteIdParam = RouteData.Values["siteId"];
                    var perIdParam = RouteData.Values["perId"];
                    if (perIdParam != null && PerId == 0)
                    {
                        int.TryParse(perIdParam.ToString(), out PerId);
                    }
                    if (siteIdParam != null && SiteId == 0)
                    {
                        int.TryParse(siteIdParam.ToString(), out SiteId);
                        if (SiteId > 0)
                        {
                            var pers = _permissionRepository.GetListByUserAndSite(UserId, SiteId);
                            if (pers.Count > 0)
                            {
                                ViewBag.PermissionJson = JsonConvert.SerializeObject(pers);
                            }
                        }
                    }
                    if(SiteId>0&&PerId==0)
                    {
                        
                    }
                    if (SiteId > 0 && PerId > 0)
                    {

                    }
                }                
            }
            base.OnActionExecuting(filterContext);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AdminBaseController"/> class.
        /// </summary>
        public AdminBaseController()
        {
            _siteRepository = new SiteRepository();
            _permissionRepository = new PermissionRepository();
        }

        /// <summary>
        /// Indexes the specified site identifier.
        /// </summary>
        /// <param name="siteId">The site identifier.</param>
        /// <returns></returns>
        public virtual ActionResult Index(int? siteId = 0)
        {
            if (siteId > 0)
            {
                var obj = _siteRepository.Get((int)siteId);
                return View(obj.ToModel());
            }
            return View("Error");
        }

    }
}
